package com.lanou.myim.ui.fragment;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMChatManager;
import com.lanou.myim.R;
import com.lanou.myim.tools.CustomDialog;
import com.lanou.myim.tools.ParseSingleton;
import com.lanou.myim.ui.activity.CreateQRCodeActivity;
import com.lanou.myim.ui.activity.LogInActivity;
import com.lanou.myim.ui.activity.PersonalDataActivity;
import com.lanou.myim.ui.activity.ScannerActivity;
import com.lanou.myim.ui.activity.SetActivity;

/**
 * Created by dllo on 16/3/3.
 */
public class PersonalFragment extends BaseFragment {
    private TextView textView;
    private RelativeLayout rbSet;
    private ImageView createCode;
    private ImageView personalData;
    private RelativeLayout logOut;
    private RelativeLayout scanner;
    private TextView tvUserName;
    private String userName;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_personal, null);
        rbSet = (RelativeLayout) view.findViewById(R.id.rb_set);
        logOut = (RelativeLayout) view.findViewById(R.id.log_out);
        tvUserName = (TextView) view.findViewById(R.id.tv_user_name);
        personalData = (ImageView) view.findViewById(R.id.personal_head);
        scanner = (RelativeLayout) view.findViewById(R.id.rb_scanner);
        createCode = (ImageView) view.findViewById(R.id.create_code);
        userName = EMChatManager.getInstance().getCurrentUser();
        tvUserName.setText(userName);
        //获取头像昵称
        ParseSingleton.setDescr(userName,tvUserName);
        ParseSingleton.setBitmap(userName,personalData);
        rbSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), SetActivity.class);
                startActivity(intent);
            }
        });
        logOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog1_1();
            }
        });
        personalData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), PersonalDataActivity.class);
                startActivity(intent);
            }
        });
        scanner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), ScannerActivity.class);
                startActivity(intent);
            }
        });
        createCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(getActivity(),CreateQRCodeActivity.class);
                intent.putExtra("userName",tvUserName.getText());
                startActivity(intent);

            }
        });
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        ParseSingleton.setDescr(userName, tvUserName);
        ParseSingleton.setBitmap(userName.toString(), personalData);


    }

    private void dialog1_1() {
        CustomDialog.Builder builder = new CustomDialog.Builder(getActivity());

        builder.setMessage("确认退出?");
        builder.setTitle("退出登录");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                EMChatManager.getInstance().logout();
                Intent intent = new Intent(getActivity(), LogInActivity.class);
                startActivity(intent);
                dialog.dismiss();
                getActivity().finish();
                //设置你的操作事项
            }
        });

        builder.setNegativeButton("取消",
                new android.content.DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });

        builder.create().show();
    }

}
